<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <!-- 引入 Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
    <!-- 引入 Font Awesome 图标库 -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <script>
        function checkError() {
            <c:if test="${not empty error}">
            alert('${error}');
            </c:if>
        }

        // 数据校验函数
        function validateForm() {
            let isValid = true;

            // 校验卡号
            const number = document.getElementById('number').value.trim();
            if (number === '') {
                showError('number', '卡号不能为空');
                isValid = false;
            } else if (!/^1\d{10}$/.test(number)) {
                showError('number', '手机卡号格式不正确');
                isValid = false;
            } else {
                hideError('number');
            }

            // 校验密码
            const password = document.getElementById('mima').value.trim();
            if (password === '') {
                showError('mima', '密码不能为空');
                isValid = false;
            } else {
                hideError('mima');
            }

            return isValid;
        }

        // 显示错误信息
        function showError(elementId, message) {
            const element = document.getElementById(elementId);
            const errorDiv = document.createElement('div');
            errorDiv.id = `${elementId}Error`;
            errorDiv.className = 'text-danger mt-1';
            errorDiv.innerHTML = message;

            // 移除旧的错误信息
            const oldErrorDiv = document.getElementById(`${elementId}Error`);
            if (oldErrorDiv) {
                oldErrorDiv.remove();
            }

            // 添加新的错误信息
            element.parentNode.insertBefore(errorDiv, element.nextSibling);

            // 添加错误样式
            element.classList.add('is-invalid');
        }

        // 隐藏错误信息
        function hideError(elementId) {
            const element = document.getElementById(elementId);
            const errorDiv = document.getElementById(`${elementId}Error`);

            if (errorDiv) {
                errorDiv.remove();
            }

            // 移除错误样式
            element.classList.remove('is-invalid');
        }

        // 为输入框添加失去焦点时的校验
        document.addEventListener('DOMContentLoaded', function() {
            document.getElementById('number').addEventListener('blur', function() {
                const number = this.value.trim();
                if (number === '') {
                    showError('number', '卡号不能为空');
                } else if (!/^1\d{10}$/.test(number)) {
                    showError('number', '手机卡号格式不正确');
                } else {
                    hideError('number');
                }
            });

            document.getElementById('mima').addEventListener('blur', function() {
                const password = this.value.trim();
                if (password === '') {
                    showError('mima', '密码不能为空');
                } else {
                    hideError('mima');
                }
            });

            // 显示后端校验错误
            <c:if test="${not empty errors}">
            <c:forEach var="e" items="${errors}">
            <c:choose>
            <c:when test="${e.objectName == 'user' && e.field == 'number'}">
            showError('number', '${e.defaultMessage}');
            </c:when>
            <c:when test="${e.objectName == 'user' && e.field == 'mima'}">
            showError('mima', '${e.defaultMessage}');
            </c:when>
            </c:choose>
            </c:forEach>
            </c:if>
        });
    </script>
    <style>
        body {
            background-color: #f4f6f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background-color: #fff;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }
        .login-container h1 {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #555;
        }
        .form-group input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .form-group input:focus {
            border-color: #007bff;
            outline: 0;
            box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
        }
        .btn-login {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            border: none;
            border-radius: 4px;
            color: #fff;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        .btn-login:hover {
            background-color: #0056b3;
        }

        /* 添加错误输入框样式 */
        .is-invalid {
            border-color: #dc3545;
            padding-right: calc(1.5em + 0.75rem);
            background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23dc3545'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23dc3545' stroke='none'/%3e%3c/svg%3e");
            background-repeat: no-repeat;
            background-position: right calc(0.375em + 0.1875rem) center;
            background-size: calc(0.75em + 0.375rem) calc(0.75em + 0.375rem);
        }
    </style>
</head>
<body onload="checkError()">
<!-- 显示后端校验错误 -->
<c:if test="${not empty errors}">
    <div class="alert alert-danger mb-4" role="alert">
        <c:forEach var="e" items="${errors}">
            ${e.defaultMessage}<br>
        </c:forEach>
    </div>
</c:if>

<div class="login-container">
    <h1>用户登录</h1>
    <form action="/springmvc02_ssm/login" method="post" onsubmit="return validateForm()">
        <div class="form-group">
            <label for="number">卡号</label>
            <input type="text" id="number" name="number" required>
        </div>
        <div class="form-group">
            <label for="mima">密码</label>
            <input type="password" id="mima" name="mima" required>
        </div>
        <button type="submit" class="btn-login">登录</button>
    </form>
</div>
<!-- 引入 Bootstrap JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>
</html>